<?php

namespace app\api\controller\admin\setting;

use app\api\services\admin\setting\UserServices;
use think\App;
use app\api\common\controller\BaseApi;
use app\Request;
use think\facade\Config;

/**
 * @Resource("user")
 */
class UserController extends BaseApi
{

    /**
     * @var UserServices
     */
    private UserServices $services;

    public function __construct(Request $request, App $app, UserServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * index  资源列表
     *
     * @param \app\Request $request
     *
     * @return \think\response\Json
     */
    public function select(Request $request): \think\response\Json
    {
        $where = $request->getParams([
            ['name', '', '', 'name_like'],
            ['account'],
            ['roles'],
            ['delete_time', null],
            ['status'],
        ]);
        $this->validate($where, [
            'name_like' => 'max:32',
        ], [], true, true);

        $ret = $this->services->select($where);

        return ret_value(0, "ok", $ret['list'], 200, [], ["count" => $ret['count']]
        );
    }

    /**
     * save  保存新建资源
     *
     * @param \app\Request $request
     *
     * @return \think\response\Json
     */
    public function insert(Request $request): \think\response\Json
    {
        $data = $request->getParams([
            ['account', ''],
            ['real_name', ''],
            ['password', '', '', 'pwd'],
            ['roles'],
        ]);

        $this->validate($data, [
            'account'   => 'require|alphaNum|length:4,32',
            'pwd'       => 'require|length:6,32',
            'real_name' => 'require|chsDash|max:32',
            'roles'     => 'require',
        ]);

        if ($data['pwd'] === '')
            unset($data['pwd']);
        else {
            $lms_common  = Config::get('lms_common');
            $data['pwd'] = md5($lms_common["password_salt"] . $data['pwd']);
        }
        $this->services->save($data);
        return ret_value(0, "Success");
    }

    /**
     * read  显示指定资源
     *
     * @param $id
     *
     * @return \think\response\Json
     */
    public function get($id): \think\response\Json
    {
        $where = ['id' => $id];
        $ret   = $this->services->get($where, ["id, account, real_name, roles, last_time, last_ip, status"])->toArray();
        return ret_value(0, "", $ret);
    }

    /**
     * update  保存更新资源
     *
     * @param \app\Request $request
     *
     * @return \think\response\Json
     */
    public function update(Request $request): \think\response\Json
    {
        $where = $request->getParams([['id']]);
        $data  = $request->getParams([
            ['id'],
            ['real_name'],
            ['password', '', '', 'pwd'],
            ['roles'],
            ['status'],
        ]);
        $this->validate($where, [
            'id' => 'require|integer',
        ]);

        $this->validate($data, [
            'pwd'       => 'length:6,32',
            'real_name' => 'chsDash|max:32',
            'status'    => 'in:0,1',
        ]);

        if ($data['pwd'] === '')
            unset($data['pwd']);
        else {
            $lms_common  = Config::get('lms_common');
            $data['pwd'] = md5($lms_common["password_salt"] . $data['pwd']);
        }

        $ret = $this->services->update($where, $data);
        if ($ret) {
            return ret_value(0, "Success");
        } else {
            return ret_value(-1, "Fail");
        }
    }

    /**
     * delete  删除指定资源
     *
     * @param \app\Request $request
     *
     * @return \think\response\Json
     */
    public function delete(Request $request): \think\response\Json
    {
        $data = $request->getParams([
            ['data'],
        ]);
        $this->validate($data, [
            'data' => 'require',
        ]);
        $ids = explode(',', $data['data']);
        $ret = $this->services->destroy($ids);
        if ($ret)
            return ret_value(0, "Success");
        else
            return ret_value(-1, "Fail");
    }
}
